package cn.tedu.blackhorse.service.impl;

import cn.tedu.blackhorse.ex.ServiceCode;
import cn.tedu.blackhorse.ex.ServiceException;
import cn.tedu.blackhorse.mapper.StudentTestMapper;
import cn.tedu.blackhorse.pojo.dto.StudentTestAddNewDTO;
import cn.tedu.blackhorse.pojo.entity.StudentTest;
import cn.tedu.blackhorse.pojo.vo.StudentTestListItemVO;
import cn.tedu.blackhorse.pojo.vo.StudentTestStandardVO;
import cn.tedu.blackhorse.service.IStudentTestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Slf4j
@Service
public class StudentTestServiceImpl implements IStudentTestService {
    @Autowired
    private StudentTestMapper studentTestMapper;

    @Override
    public void addNew(StudentTestAddNewDTO studentTestAddNewDTO) {
        StudentTestStandardVO studentTestStandardVO = studentTestMapper.getById(studentTestAddNewDTO.getStudentId());
        if (studentTestStandardVO != null){
            String message = "添加失败,学生id不存在!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        StudentTest studentTest = new StudentTest();
        BeanUtils.copyProperties(studentTestAddNewDTO,studentTest);
        int row = studentTestMapper.insert(studentTest);
        if (row != 1){
            String message = "添加失败,系统繁忙请稍后再试!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
    }

    @Override
    public List<StudentTestListItemVO> list() {
        return studentTestMapper.list();
    }
}
